package com.pegasus.astrologiaEnergiaDivina.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.pegasus.astrologiaEnergiaDivina.bean.ItemCalendarioAscendente;
import com.pegasus.astrologiaEnergiaDivina.bean.ItemCalendarioLunarBean;
import com.pegasus.astrologiaEnergiaDivina.bean.ItemCalendarioSolarBean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class SignoDao {
	private SQLiteDatabase db;
	private DatabaseHelper helper;

	public SignoDao(Context context) throws IOException {
		helper = new DatabaseHelper(context);
		db = helper.getDatabase();
		
	}

	public List<ItemCalendarioLunarBean> getItensCalendarioLunar(long ano, long mes) {


		Cursor cursor = db.query(
				"SIGNO_LUNAR",null,"ano >= "+(ano-1)+" and ano <= "+(ano+1), null, null, null, "cast(ano as integer), cast (mes as integer), cast(dia as integer)");
		List<ItemCalendarioLunarBean> lista = new ArrayList<ItemCalendarioLunarBean>();
		ItemCalendarioLunarBean item;
		while(cursor.moveToNext()){
			item = new ItemCalendarioLunarBean();
			
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.MONTH, (cursor.getInt(0)-1));
			cal.set(Calendar.YEAR, cursor.getInt(1));
			cal.set(Calendar.DAY_OF_MONTH, cursor.getInt(2));
			
			if(cursor.getString(3).length()==5){
				cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cursor.getString(3).substring(0, 2)));
				cal.set(Calendar.MINUTE, Integer.parseInt(cursor.getString(3).substring(3, 5)));
			}
			
			item.setData(cal);
			item.setSigno(cursor.getString(4));
			
			lista.add(item);
		}
		return lista;
	}
	
	public List<ItemCalendarioSolarBean> getItensCalendarioSolar() {

		Cursor cursor = db.query(
				"SIGNO_SOLAR",null,null, null, null, null, null);
		List<ItemCalendarioSolarBean> lista = new ArrayList<ItemCalendarioSolarBean>();
		ItemCalendarioSolarBean item;
		while(cursor.moveToNext()){
			item = new ItemCalendarioSolarBean();
			item.setSigno(cursor.getString(0));			
			
			Calendar dtInicio = Calendar.getInstance();
			if(cursor.getString(1).length()==5){
				dtInicio.set(Calendar.DAY_OF_MONTH, Integer.parseInt(cursor.getString(1).substring(0, 2)));
				dtInicio.set(Calendar.MONTH, (Integer.parseInt(cursor.getString(1).substring(3, 5)))-1);
				item.setDataInicial(dtInicio);
			}
		
			
			Calendar dtFinal = Calendar.getInstance();
			if(cursor.getString(2).length()==5){
				dtFinal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(cursor.getString(2).substring(0, 2)));
				dtFinal.set(Calendar.MONTH, (Integer.parseInt(cursor.getString(2).substring(3, 5)))-1);
				item.setDataFinal(dtFinal);
			}
			
			
			lista.add(item);
		}
		return lista;
	}	
	
	public List<ItemCalendarioAscendente> getItensCalendarioAscendente(String signoSolar) {

		Cursor cursor = db.query(
				"ASCENDENTE",null,"SIGNO_SOLAR = '"+signoSolar+"'", null, null, null, "HR_INICIO");
		List<ItemCalendarioAscendente> lista = new ArrayList<ItemCalendarioAscendente>();
		ItemCalendarioAscendente item;
		while(cursor.moveToNext()){
			item = new ItemCalendarioAscendente();
			Calendar hrInicial = Calendar.getInstance();
			Calendar hrFinal = Calendar.getInstance();
			
			if(cursor.getString(1).length()==5){
				hrInicial.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cursor.getString(1).substring(0, 2)));
				hrInicial.set(Calendar.MINUTE, Integer.parseInt(cursor.getString(1).substring(3, 5)));
				item.setHoraInicial(hrInicial);
			}
			
			if(cursor.getString(2).length()==5){
				hrFinal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cursor.getString(2).substring(0, 2)));
				hrFinal.set(Calendar.MINUTE, Integer.parseInt(cursor.getString(2).substring(3, 5)));
				item.setHoraFinal(hrFinal);
			}
			item.setAscendente(cursor.getString(3));	
			
			lista.add(item);
		}
		return lista;
	}
}
